package edu.sbcc.cs123.animationsimpletween;

import java.util.*;

import edu.sbcc.cs123.animationsimpletween.R.*;
import android.app.*;
import android.os.*;
import android.view.*;
import android.view.ViewGroup.*;
import android.view.animation.*;
import android.view.animation.Animation.*;
import android.widget.*;
import android.widget.TableLayout.*;

public class AnimationSimpleTweenActivity extends Activity implements View.OnClickListener, AnimationListener {

	private ImageView droidView;				// Who uses a droid anyway?
	private Random rng = new Random();	// Random number generator to compute hyperspace destination.

	private Animation jumpOut;		// This is the hyperspace animation.
	private RelativeLayout mainLayout;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
		droidView = (ImageView) findViewById(R.id.droidView);
		droidView.setOnClickListener(this);

	}


	@Override
	public void onClick(View v) {
		jumpOut = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
		jumpOut.setAnimationListener(this);
		droidView.startAnimation(jumpOut);
	}


	@Override
	public void onAnimationEnd(Animation animation) {

		if (animation == jumpOut) {

			// Randomly select a new position on the screen.
			int toX = (int) (rng.nextDouble() * (mainLayout.getWidth() - droidView.getWidth()));
			int toY = (int) (rng.nextDouble() * (mainLayout.getHeight() - droidView.getHeight()));
			
			// Move the droid to the new position on the screen.  What a pain that there isn't a setLeft(), setTop(), or setLocation().
			RelativeLayout.LayoutParams rlp = (RelativeLayout.LayoutParams) droidView.getLayoutParams();
			rlp.leftMargin = toX;
			rlp.topMargin = toY;
			droidView.setLayoutParams(rlp);
		}
		
	}


	@Override
	public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		
	}

	
}